<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- -------------------------------------------------------------------------------------- -->
<script src="http://code.jquery.com/jquery-latest.js"></script>

<script type="text/javascript">
$(document).ready(function() {    
	$("#writeBtn").click(function(){openWriteBoard();});
	$("#searchBoardbtn").click(function(){searchBoard();});//id가 searchBoardbtn인 것을 클릭하면 searchBoard() function을 실행
});

function openWriteBoard(){
	$("#writeBoardForm").submit();
}
function deleteBoard(i){
	var url = '/${initParam.context_root }/deleteBoard.do?boardNum='+i;
	var flag = window.confirm("정말 삭제하시겠습니까?");//예 : ture, 아니오 : false
	if(flag){
		$("#removeBoard").attr({"action":url,"method":"post"}).submit();//id가 removeBoard인 폼의 action 속성을 url값으로 변경, method 속성을 post값으로 변경하여 submit()실행
	}
	else{
		return;
	}
}
function searchBoard(){
	var temp = document.getElementById("searchType");//변수 temp에 선택된 searchType을 탐색한 결과를 저장
	var selectedType = temp.selectedIndex;//변수 selectedType에 선택된 selectBox의 인덱스값을 저장
	if(selectedType==0){//selectBox의 첫번째 요소 선택시
		alert("검색조건을 선택하세요");
		return;
	}
	var url = "/${initParam.context_root }/searchBoard.do";
	$("#searchBoardForm").attr({"action":url, "method":"posst"}).submit(); //id가 searchBoardForm인 폼의 action 속성을 url값으로 변경, method 속성을 post값으로 변경하여 submit()실행
}
</script>
<!-- -------------------------------------------------------------------------------------- -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>CRUD alpha</title>
</head>
<body>
	${message }
	<form id="searchBoardForm">
		<div align="right">
			<select name="searchType" id="searchType">
				<option value="0">선택하세요</option>
				<option value="regName">글쓴이</option>
				<option value="subject">제목</option>
			</select>&nbsp; <input type="text" name="searchText" maxlength="30"> <input
				type="button" id="searchBoardbtn" value="검색">
		</div>
	</form>
	<table border="1" cellpadding="4px" cellspacing="0" width="100%">
		<thead>
			<tr bgcolor="lightgray">
				<th width="10%">no</th>
				<th width="40%">제목</th>
				<th width="20%">글쓴이</th>
				<th width="20%">등록일</th>
				<th width="10%">삭제</th>
			</tr>
		</thead>
		<tbody>
			<c:choose>
				<c:when test="${not empty list.boardList}">
					<c:forEach items="${list.boardList }" var="boardVO" varStatus="i">
						<tr>
							<td>${boardVO.num }</td>
							<td><a
								href="/${initParam.context_root }/board.do?num=${boardVO.num}">${boardVO.subject}</a></td>
							<td>${boardVO.regName }</td>
							<td>${boardVO.regDate }</td>
							<td align="center">
								<form id="removeBoard">
									<input type="button" value="삭제"
										onClick="deleteBoard(${boardVO.num })">
								</form>
							</td>
						</tr>
					</c:forEach>
				</c:when>
				<c:otherwise>
					<tr>
						<td colspan="5">등록된 게시물이 없습니다.<c:out
								value="${fn:length(list.boardList)} "></c:out></td>
					</tr>
				</c:otherwise>
			</c:choose>
		</tbody>
	</table>
	<br>
	<center>
		<table>
			<tr>
				<td><c:choose>
						<c:when test="${list.paging.previousPageGroup }">
							<a
								href="/${initParam.context_root }/mainBoard.do?page=${list.paging.currPage -1}">◀</a>
						</c:when>
						<c:otherwise>◀</c:otherwise>
					</c:choose> <c:forEach begin="${list.paging.startPageOfPageGroup }"
						end="${list.paging.endPageOfPageGroup }" step="1" var="i">
						&nbsp;
						<font color=orange> <B> <a
								href="/${initParam.context_root }/mainBoard.do?page=${i}">${i}</a>
						</B>
						</font>&nbsp;&nbsp;	
					</c:forEach> <c:choose>
						<c:when test="${list.paging.nextPageGroup }">
							<a
								href="/${initParam.context_root }/mainBoard.do?page=${list.paging.currPage +1 }">▶</a>
						</c:when>
						<c:otherwise>▶</c:otherwise>
					</c:choose></td>
			</tr>
		</table>
	</center>
	<br>
	<form id="writeBoardForm" name="writeBoardForm"
		action="/${initParam.context_root }/writeBoard.do" method="post"
		enctype="multipart/form-data">
		<div align="right">
			<button id="writeBtn" title="글쓰기">글쓰기</button>
		</div>
	</form>

</body>
</html>